"""
    Author:
    Date: 2019-01-21

    微信管理后台-下载模块
"""

from django.conf import settings
from django.shortcuts import render

from entities import version_mgr

from .. import helpers
from utils import util


def app_version_list(request):
    """ 测试环境的版本列表 """
    is_ios_device = util.is_ios_device(request)
    # platforms = '"ios","ios_ee"' if is_ios_device else '"android"'
    platforms = 'ios' if is_ios_device else 'android'

    versions = version_mgr.get_version_list(platforms)
    for version in versions:
        platform = version['platform'].lower()
        # if platform in ['ios', 'ios_ee']:
        #     ios_download_url = version_mgr.fetch_ios_plist_file(version['build_no'], version['download_url'])
        #     version['download_url'] = ios_download_url

        platform_name = platform
        if platform == 'ios':
            platform_name = '苹果商店版'
        elif platform == 'ios_ee':
            platform_name = '企业版'
        version['platform_name'] = platform_name

    context = helpers.build_context(request)
    context['versions'] = versions
    context['is_ios_device'] = is_ios_device
    return render(request, 'gma/download/version_list.html', context)


def download_app(request, app=None):
    """ 下载指定的测试版本 """
    config = version_mgr.get_version_config(settings.OFFICIAL_IOS_CHANNEL, settings.OFFICIAL_ANDROID_CHANNEL)

    if config.get('ios_download_url', None) is None and config.get('android_download_url', None) is None:
        return helpers.show_page_tips(request, f'没有找到任何版本的配置信息!')

    context = helpers.build_context(request)
    context['config'] = config
    return render(request, 'gma/download/download_app.html', context)


if __name__ == '__main__':
    print('ok')
